package bookticket.crm.ticket.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bookticket.crm.ticket.entity.TicketEntity;
import bookticket.crm.ticket.service.TicketService;

@Controller
@RequestMapping("/ticket")
public class TicketController {

	@Autowired
	private TicketService service;
	
	@ResponseBody
	@RequestMapping("/findAll")
	public Map<String,Object> findAll(Integer page,Integer rows){
		
		Integer before = (page-1)*rows;
		Integer after = rows;
		
		Map<String, Object> pageMap = new HashMap<String,Object>();
		pageMap.put("before", before);
		pageMap.put("after", after);
		
		List<TicketEntity> list = service.findAll(pageMap);
		Map<String, Object> map = new HashMap<String,Object>();
		
		//分页查询中固定的写法  total rows
		int total = service.getTotalCounter();
		map.put("total", total);
		map.put("rows", list);
		map.put("result", "success");
		
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/add")
	public Map<String,String> add(TicketEntity ticketEntity){
		Map<String,String> map = new HashMap<String,String>();
		int counter = service.add(ticketEntity);
		if(counter == 1){
			map.put("msg", "增加成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "增加失败");
			map.put("result","failure" );
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping("/deleteByIds")
	//@ResponseBody把所有的json对象转化成java对象
	public Map<String,String> deleteByIds(@RequestBody List<Integer> ids){
		
		Map<String,String> map = new HashMap<String,String>();
		int counter = service.deleteByIds(ids);
		if(counter > 0){
			map.put("msg", "删除成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "删除失败");
			map.put("result","failure" );
		}
		return map;
	}
	
	/*@ResponseBody
	@RequestMapping("/update")
	public Map<String,String> update(TicketEntity ticketEntity){
		Map<String,String> map = new HashMap<String,String>();
		int counter = service.update(ticketEntity);
		if(counter == 1){
			map.put("msg", "修改成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "修改失败");
			map.put("result","failure" );
		}
		return map;
	}*/
	
	@ResponseBody
	@RequestMapping("/update")
	public Map<String,String> update(TicketEntity ticketEntity){
		Map<String,String> map = new HashMap<String,String>();
		int counter = service.update(ticketEntity);
		System.err.println("counter:"+counter);
		if(counter == 1){
			map.put("msg", "修改成功");
			map.put("result","success" );
		}
		else{
			map.put("msg", "修改失败");
			map.put("result","failure" );
		}
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/findById")
	public TicketEntity findById(Integer id){
		TicketEntity ticketEntity = service.findById(id);
		return ticketEntity;
	}
}
